package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mapper.BlackListMapper;
import com.tanhua.dubbo.mapper.SettingsMapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author LiKe
 * @date 2022/1/22 10:27
 */
@DubboService
public class SettingsApiImpl implements SettingsApi {
    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    @Override
    public Settings findByUserId(Long userId) {
        LambdaQueryWrapper<Settings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Settings::getUserId,userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(Settings settings) {
        settingsMapper.insert(settings);
    }

    @Override
    public void update(Settings settings) {
        settingsMapper.updateById(settings);
    }

    @Override
    public PageResult blackLimit(Long userId,int page,int size){
        //查询总记录数
        int blackCount = blackListMapper.findBlackCount(userId);
        //总页数 = 总记录数 / size Math.ceil()
        int pages = (int) Math.ceil(blackCount/size);
        //总黑名单用户信息 10  2 4  4
        int start = (page-1)*size;
        List<UserInfo> list = userInfoMapper.findByIdBlackLimit(userId, start, size);
        return new PageResult(blackCount,size,pages,page,list);
    }
}
